# src/server/minio_client.py
import io
import os

from minio import Minio
import uuid

MINIO_ENDPOINT = os.getenv("MINIO_ENDPOINT", "47.108.188.77:9000")
MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "vora")
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "1qaz@WSX")
MINIO_BUCKET = os.getenv("MINIO_BUCKET", "resumes")

# 创建 MinIO 客户端
minio_client = Minio(
    MINIO_ENDPOINT,
    access_key=MINIO_ACCESS_KEY,
    secret_key=MINIO_SECRET_KEY,
    secure=False,
)

# 确保 bucket 存在
if not minio_client.bucket_exists(MINIO_BUCKET):
    minio_client.make_bucket(MINIO_BUCKET)


def upload_file_to_minio(file_obj: io.BytesIO, object_name: str, content_type: str) -> str:
    file_obj.seek(0, 2)  # 移动到末尾
    length = file_obj.tell()  # 获取当前指针位置作为长度
    file_obj.seek(0)  # 重新回到开头

    minio_client.put_object(
        bucket_name=MINIO_BUCKET,
        object_name=object_name,
        data=file_obj,
        length=length,
        content_type=content_type,
    )

    return f"http://{MINIO_ENDPOINT}/{MINIO_BUCKET}/{object_name}"
